import axios from 'axios'
import store from '../store/index'

export function UploadFile(url,data,callback){
    axios({
        url,
        method:"post",
        headers: { 
            "Content-Type": "multipart/form-data",
            "Authorization" :store.state.token
        },
        data
    }).then(res=>{
        callback(res);
    }).catch(res=>{
        callback(res);
    })
}

export function DownLoadFile(url,data){
    axios({
        url,
        method:"post",
        responseType: 'blob',
        headers: { 
            "Authorization" :store.state.token
        },
        params:data
    }).then(res=>{
        let blob = new Blob([res.data], {
            type: 'application/vnd.openxml;formats-office;document.spread;sheetml.sheet;charset=utf-8'
        })
        let href = window.URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.href = href ;
        a.download = res.headers.filename;
        a.click();
        window.URL.revokeObjectURL(href);
    }).catch(res=>{
        console.log(res)
    })
}


export async function GetImage(url){
    return await axios({
        url,
        method:"get",
        responseType: 'blob',
        headers: { 
            "Authorization" :store.state.token
        }
    }).then(res=>{
        let blob = new Blob([res.data])
        let href = window.URL.createObjectURL(blob);
        return href;
    }).catch(res=>{
        console.log(res)
    })
}